package com.mall4j.cloud.product.controller.ua;

import com.mall4j.cloud.api.product.vo.CategoryVO;
import com.mall4j.cloud.common.response.ServerResponseEntity;
import com.mall4j.cloud.product.dto.CategoryDTO;
import com.mall4j.cloud.product.service.CategoryService;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;

/**
 * app分类
 *
 * @author JCM
 * @date 2023-04-22 13:09:00
 */
@RestController("uaCategoryController")
@RequestMapping("/ua/category")
public class CategoryController {

    @Autowired
    private CategoryService categoryService;

    @GetMapping("/category_list")
    @ApiOperation(value = "获取指定分类下的分类列表",notes = "获取指定分类下的分类列表")
    public ServerResponseEntity<List<CategoryVO>> category_list(CategoryDTO categoryDTO) {
        return ServerResponseEntity.success(categoryService.list(categoryDTO));
    }

    @GetMapping("/shop_category_list")
    @ApiOperation(value = "获取指定分类下的分类列表",notes = "获取指定分类下的分类列表")
    public ServerResponseEntity<List<CategoryVO>> shop_category_list(CategoryDTO categoryDTO) {
        //根据得到的顶级父类获得其下子类
        List<CategoryVO> categoryVOS = categoryService.list(categoryDTO);
        for (CategoryVO categoryVO : categoryVOS){
            List<CategoryVO> firstChildCategories = categoryService.getByParentId(categoryVO.getCategoryId());
            for (CategoryVO firstChildCategory : firstChildCategories){
                List<CategoryVO> secondChildCategories = categoryService.getByParentId(firstChildCategory.getCategoryId());
                firstChildCategory.setCategories(secondChildCategories);
            }
            categoryVO.setCategories(firstChildCategories);
        }
        return ServerResponseEntity.success(categoryVOS);
    }
}
